﻿using System;

namespace Manox.Libs.Networking
{
    /// <summary>
    /// Unity network action interface.
    /// </summary>
    public interface INetworkAction
    {
        /// <summary>
        /// Process data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connectionId"></param>
        /// <param name="message"></param>
        void ProcessData(object sender, NetworkConnectionId connectionId, NetworkMessage message);
    }

    /// <summary>
    /// Unity network action.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class NetworkAction<T> : INetworkAction, IEquatable<NetworkAction<T>> where T : NetworkMessage
    {
        /// <summary>Action.</summary>
        private Action<object, NetworkConnectionId, T> Action;


        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="action"></param>
        public NetworkAction(Action<object, NetworkConnectionId, T> action)
        {
            this.Action = action;
        }

        /// <summary>
        /// Process data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connectionId"></param>
        /// <param name="message"></param>
        public void ProcessData(object sender, NetworkConnectionId connectionId, NetworkMessage message)
        {
            this.Action(sender, connectionId, (T)message);
        }

        /// <summary>
        /// Equals.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(NetworkAction<T> other)
        {
            return this.Action == other.Action;
        }
    }
}
